package de.fhb.itunes.controller.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Enumeration;

import javax.imageio.stream.FileImageInputStream;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class player
 */
@WebServlet("/player")
public class player extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public player() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8");
		Enumeration en = request.getParameterNames();
		String path = "";
		while (en.hasMoreElements()) {
			String paramName = (String) en.nextElement();
			String paraValue = request.getParameter(paramName);
			if (paramName.contains("path")) {
				path = paraValue;
				path = path.replaceFirst("'", "");
				int zeichen = path.lastIndexOf('\'');
				path = path.substring(0, zeichen);
			}

		}
		if (!path.equalsIgnoreCase("")) {
			File file = new File(path);

			int dot = path.lastIndexOf('.');
			String extension = path.substring(dot + 1);
			if (extension.equalsIgnoreCase("m4a")) {
				response.setHeader("Content-Type", "audio/mp4");
			} else if (extension.equalsIgnoreCase("mp3")) {
				response.setHeader("Content-Type", "audio/mpeg");
			} else if (extension.equalsIgnoreCase("mp4")) {
				response.setHeader("Content-Type", "video/mpeg");
			} else {
				response.setHeader("Content-Type", "application/octet-stream");
			}
			// response.setHeader("Content-Length", file.length());
			response.setHeader("Content-Disposition", "inline; filename=\""
					+ file.getName() + "\"");

			BufferedInputStream input = null;
			BufferedOutputStream output = null;

			try {
				input = new BufferedInputStream(new FileInputStream(file));
				output = new BufferedOutputStream(response.getOutputStream());

				byte[] buffer = new byte[8192];
				for (int length = 0; (length = input.read(buffer)) > 0;) {
					output.write(buffer, 0, length);
				}
			} finally {
				if (output != null)
					try {
						output.close();
					} catch (IOException logOrIgnore) {
					}
				if (input != null)
					try {
						input.close();
					} catch (IOException logOrIgnore) {
					}
			}

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
